package javaclass.trouble;

public class ServiceA {
    public ServiceA[] ses;
    private int i;
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ServiceA [i=");
        builder.append(i);
        builder.append("]");
        return builder.toString();
    }
    private ServiceA() {};
    private ServiceA(int i) {
        System.out.println("new" + i);
        this.i = i;
        ses = new ServiceA[100000];
        for(i=0;i<100000;i++) {
            ses[i] = new ServiceA();
            ses[i].i = i;
        }
        System.out.println("new——end");
    }
    public void say() {
        System.out.println("say:"+i);
    }
    public static ServiceA getS() {
        return s;
    }
    public volatile static ServiceA s = null;
    public static ServiceA get(int i) {
        if(s == null) {
            System.out.println("wait...");
            synchronized(ServiceA.class) {
                if(s == null) {
                    s = new ServiceA(i);
                } 
            }
        }
        return s;
    }
    public static void init() {
        s = null;
    }
    public static void go() {
        ServiceA s100 = get(-100);
        if(s100 != null) {
            s.ses[99999].say();
        }
    }
}
